/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mapping;
import entreprise.Reservation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import mapping.SqlLib;
/**
 *
 * @author Quoc-Khai
 */
public class DBReservation {
    public Reservation getReservation(String reservationID){
        Reservation iReservation = new Reservation();
        String selectQuery;
        selectQuery = "SELECT * FROM reservation WHERE reservationID="+reservationID;
        
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);
            
            try {
                while(sqlResultSet.next()) {
                    iReservation.setReservationID(sqlResultSet.getString("reservationID"));
                    iReservation.setPatientName(sqlResultSet.getString("patientName"));
                    iReservation.setPatientPhoneNumber(sqlResultSet.getString("patientPhoneNumber"));
                    iReservation.setPatientEmail(sqlResultSet.getString("patientEmail"));
                    iReservation.setReservationTime(sqlResultSet.getDate("reservationTime"));
                    iReservation.setRequire(sqlResultSet.getString("require"));
                }
            } catch (SQLException ex) {
                Logger.getLogger(DBReservation.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
//            System.out.println("Reservation not found!");
        } 
        return iReservation;
    }
    public boolean createReservation(Reservation reservation){
        //TODO;
        java.sql.Date sqlDate = new java.sql.Date(reservation.getReservationTime().getTime());
        String insertQuery;
        insertQuery = "INSERT INTO reservation (patientName, patientPhoneNumber, patientEmail, reservationTime, `require`) VALUES ('" 
                + reservation.getPatientName()+ "','" 
                + reservation.getPatientPhoneNumber()+ "','" 
                + reservation.getPatientEmail()+ "','" 
                + sqlDate + "','" 
                + reservation.getRequire()
                + "')";
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(insertQuery);
//            System.out.println("Insert Reservation success!");
            return true;
        }
        return false;
    }
    public boolean updateReservation(Reservation reservation){
        //TODO
        java.sql.Date sqlDate = new java.sql.Date(reservation.getReservationTime().getTime());
        String updateQuery;
        updateQuery = "UPDATE reservation SET patientName='" + reservation.getPatientName()+ 
                "', patientPhoneNumber='" + reservation.getPatientPhoneNumber()+
                "',patientEmail='" + reservation.getPatientEmail()+ 
                "',reservationTime='" + sqlDate+ 
                "',`require`='" + reservation.getRequire()+ 
                "' WHERE reservationID="+reservation.getReservationID();
        
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(updateQuery);
//            System.out.println("Update Reservation success!");
            return true;
        }
        return false;
    }
    public boolean deleteReservation(String reservationID){
        //TODO
         String deleteQuery;
        deleteQuery = "DELETE FROM reservation WHERE reservationID="+reservationID; 
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(deleteQuery);
//            System.out.println("Delete Reservation success!");
            return true;
        }
        return false;
    }    
    public ArrayList<Reservation> getReservations(String conditions){
        ArrayList<Reservation> reservations=new ArrayList<Reservation>();
        String selectQuery="SELECT * FROM reservation";
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);
            
            try {
                while(sqlResultSet.next()) {
                    Reservation iReservation = new Reservation();
                    iReservation.setReservationID(sqlResultSet.getString("reservationID"));
                    iReservation.setPatientName(sqlResultSet.getString("patientName"));
                    iReservation.setPatientPhoneNumber(sqlResultSet.getString("patientPhoneNumber"));
                    iReservation.setPatientEmail(sqlResultSet.getString("patientEmail"));
                    iReservation.setReservationTime(sqlResultSet.getDate("reservationTime"));
                    iReservation.setRequire(sqlResultSet.getString("require"));
                    reservations.add(iReservation);
                }
            } catch (SQLException ex) {
                Logger.getLogger(DBReservation.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
//            System.out.println("Reservation not found!");
        }        
        return reservations;
    }    
}
